package com.capinfo.service.impl;

import com.capinfo.base.BaseEntity;
import com.capinfo.base.BaseMapper;
import com.capinfo.base.impl.BaseServiceImpl;
import com.capinfo.entity.SysDict;
import com.capinfo.exception.MyException;
import com.capinfo.mapper.SysDictMapper;
import com.capinfo.service.DictService;
import com.capinfo.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhuxiaomeng
 * @date 2018/1/6.
 * @email 154040976@qq.com
 */
@Service
public class DictServiceImpl extends BaseServiceImpl<SysDict,String> implements DictService {

  @Autowired
  SysDictMapper sysDictMapper;
  @Override
  public BaseMapper<SysDict, String> getMappser() {
    return sysDictMapper;
  }

  @Override
  public JsonUtil delById(String id, boolean flag) {
    if (StringUtils.isEmpty(id)) {
      return JsonUtil.error("获取数据失败");
    }
    JsonUtil j=new JsonUtil();
    try {
      SysDict sysDict = selectByPrimaryKey(id);

      if (flag) {
        //逻辑
        sysDict.setDelFlag(BaseEntity.DEL_FLAG_DELETE);
        updateByPrimaryKeySelective(sysDict);
      } else {
        //物理
        sysDict.setDelFlag(BaseEntity.DEL_FLAG_DELETE);
        updateByPrimaryKeySelective(sysDict);
//        sysDictMapper.delById(id);
      }
      j.setMsg("删除成功");
    } catch (MyException e) {
      j.setMsg("删除失败");
      j.setFlag(false);
      e.printStackTrace();
    }
    return j;

  }

  @Override
  public int checkValue(String dictType, String dictValue) {
    return sysDictMapper.checkValue(dictType,dictValue);
  }
}
